<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:dic="http://www.w3.org/1999/xhtml">
<body class="list">
<div th:replace="include/head.html"></div>
<div style="padding: 20px; background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3 layui-hide">
            <div class="layui-card">
                <div class="layui-card-header">岗位架构</div>
                <div class="layui-card-body">
                    <div class="layui-fluid  layui-anim-upbit layui-anim">
                        <div class="layui-card">
                            <!--表格中的数据 并不显示-->
                            <div class="layui-card-body">
                                <!--数据表格 -->
                                <table class="layui-hidden" id="treeTable" lay-filter="treeTable"></table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-fluid  layui-anim-upbit layui-anim">
                        <div class="layui-card">
                            <!--查询from 表单-->
                            <div class="layui-card-body">
                                <form class="layui-form" >
                                    <div class="layui-form-item">
                                        <div class="layui-inline">
                                            <label class="layui-form-label">登录账号</label>
                                            <div class="layui-input-inline">
                                                <input type="text" name="account" placeholder="请输入" autocomplete="off" class="layui-input">
                                            </div>
                                            <label class="layui-form-label">用户名</label>
                                            <div class="layui-input-inline">
                                                <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                                            </div>
                                            <div class="layui-input-inline">
                                                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="search">
                                                    <i class="layui-icon layui-icon-search"></i>
                                                    查询
                                                </button>
                                            </div>
                                        </div>
                                        <div class="layui-form layui-card-header layuiadmin-card-header-auto"></div>
                                    </div>
                                </form>
                            </div>
                            <!--<div class="layui-form layui-card-header layuiadmin-card-header-auto">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">登录账号</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="account" placeholder="请输入" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">用户名</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
                                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>-->
                            <!--表格中的数据 并不显示-->
                            <div class="layui-card-body">
                                <!--表格外部上侧的按钮-->
                                <div style="padding-bottom: 10px;" class="tool-bar">
                                    <button class="layui-btn layuiadmin-btn-useradmin" data-type="add" shiro:hasPermission="sysuser:add">添加</button>
                                </div>
                                <!--数据表格 -->
                                <table id="table" lay-filter="table"></table>
                                <script type="text/html" id="switchTpl">
                                    <input type="checkbox" name="status" value="{{d.oid}}" lay-skin="switch" lay-text="启用|禁用"
                                           lay-filter="status" {{ d.isAble == "Y" ? 'checked' : '' }}>
                                </script>
                                <!--表格每行的 的操作按钮 -->
                                <script type="text/html" id="toolbar">
                                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="resetPassword" shiro:hasPermission="sysuser:resetPassword">
                                        <i class="layui-icon layui-icon-edit"></i>重置密码</a>
                                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" shiro:hasPermission="sysuser:edit">
                                        <i class="layui-icon layui-icon-edit"></i>编辑</a>
                                    {{#  if(d.oid != '00000000000000000000000000000000' ){ }}
                                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                                       shiro:hasPermission="sysuser:delBean"><i
                                            class="layui-icon layui-icon-delete"></i>删除</a>
                                    {{#  } }}
                                </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" th:inline="none">
    var table ;
    var selectDeptOid = '';
    layui.use(['jquery','table', 'laydate','treeGrid'], function () {
        var $ = layui.$;
        table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var tableId = 'treeTable';
        var treeGrid = layui.treeGrid;//很重要
        reloadTabele(null);
        /*var ptable = treeGrid.render({
            id: tableId
            , elem: '#' + tableId
            , url: 'sysDept/json'
            , cellMinWidth: 100
            , idField: 'oid'//必須字段
            , treeId: 'oid'//树形id字段名称
            , treeUpId: 'parentOid'//树形父id字段名称
            , treeShowName: 'deptName'//以树形式显示的字段
            , heightRemove: [".dHead", 0]//不计算的高度,表格设定的是固定高度，此项不生效
            , height: 'full-160'
            , isFilter: false
            , iconOpen: false//是否显示图标【默认显示】
            , isOpenDefault: true//节点默认是展开还是折叠【默认展开】
            , loading: true
            , method: 'post'
            , isPage: false
            , cols: [[
                {field: 'deptName', title: '部门'}
            ]]
            , parseData: function (res) {//数据加载后回调
                return res;
            }
            , onClickRow: function (index, o) {
                selectDeptOid = o.oid;
                reloadTabele(o.oid);
                //console.log(index,o,"单击！");
            }
            , onDblClickRow: function (index, o) {
                //console.log(index,o,"双击");
            }
            , onCheck: function (obj, checked, isAll) {//复选事件
                //console.log(obj,checked,isAll);
            }
            , onRadio: function (obj) {//单选事件
                //console.log(obj);
            }
        });*/
        //监听搜索
        form.on('submit(search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {where: field});
        });
        //监听 按钮
        form.on('switch(status)', function(obj){
            $.post("sysuser/updateAble", {oid:this.value}, function (res) {
                if (res.code === 0) {
                    layer.msg(res.msg);
                    table.reload('table');
                } else {
                    layer.msg(res.msg);
                }
            });
            // layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        });
        //监听行工具事件
        //注：tool 是工具条事件名，table 是 table 原始容器的属性 lay-filter="对应的值"
        table.on('tool(table)', function (obj) {
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                layer.msg('查看操作');
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.post("sysuser/delBean",{oid:data.oid},function (res) {
                        //向服务端发送删除指令
                        if(res.code === 0){
                            layer.msg(res.msg);
                            obj.del(); //删除对应行（tr）的DOM结构
                            layer.close(index);
                            //table.reload('table');
                        }else {
                            layer.msg(res.msg);
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                editLayer({
                    title:'编辑用户',
                    url:'sysuser/edit?oid='+data.oid,
                    area:['800px','500px'],
                    back:callback
                });
            }else if (layEvent === 'resetPassword') {
                layer.confirm('密码将重置为 123456', function (index) {
                    $.post("sysuser/resetPassword",{oid:data.oid},function (res) {
                        //向服务端发送删除指令
                        if(res.code === 0){
                            table.reload('table');
                            layer.msg(res.msg);
                            layer.close(index);
                        }else {
                            layer.msg(res.msg);
                        }
                    });
                });
            }
        });
        $('.tool-bar .layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //事件
        var active = {
            add: function () {
                var adduser =  editLayer({
                    title:'添加用户',
                    url:'sysuser/edit?deptOid='+selectDeptOid,
                    area:['800px','500px'],
                    back: callback
                });
                //layer.full(adduser);
            }
        };


    });
    
    function reloadTabele(deptOid) {
        table.render({
            elem: '#table'
            , id: 'table'
            , loading: true
            , method: 'post'
            , url: 'sysuser/getPage' //数据接口
            , height: 'full-254'
            , where :{deptOid:deptOid}
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'numbers', title: '序号'}
                , {field: 'account', title: '登录账号'}
                , {field: 'name', title: '用户名'}
                , {field: 'status', title: '状态', templet: '#switchTpl'}
                , {field: 'userType', title: '用户类型',templet:function (d) {
                        if (d.userType == 'admin') {
                            return "<button type=\"button\" class=\"layui-btn layui-btn-xs layui-btn-normal\">管理员</button>"
                        } else  if(d.userType == 'user'){
                            return "<button type=\"button\" class=\"layui-btn layui-btn-xs layui-btn-normal\">普通用户</button>"
                        }else  if(d.userType == 'hotel'){
                            return "<button type=\"button\" class=\"layui-btn layui-btn-xs layui-btn-normal\">酒店</button>"
                        }else  if(d.userType == 'service'){
                            return "<button type=\"button\" class=\"layui-btn layui-btn-xs layui-btn-normal\">服务者</button>"
                        }else {
                            return '';
                        }
                    }}
                , {fixed: 'right', align: 'center', toolbar: '#toolbar',title:'操作'}
            ]]
        });
    }
    
    function callback(ifr,index,data){
        var field = data.field;
        $.post("sysUserRole/saveSysUser",field,function (res) {
            if(res.code === 0){
                topLayer.msg(res.msg);
                topLayer.close(index);
                table.reload('table');
            }else {
                topLayer.msg(res.msg);
            }
        });
        topLayer.close(index);
    }
</script>
</body>
</html>
